﻿Imports System.Diagnostics
Imports System.Data
Imports System.Web
Partial Class _Default
    Inherits System.Web.UI.Page
    Public quiz As Quizsln = New Quizsln()
    Private email As String
    Private bannerID As String

    ''' <summary>
    ''' Used for checking if a student is in the system through their bannerID and email
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
        lblBannerId.Text = ""
        lblEmail.Text = ""
        lblNoAttempt.Text = ""
        'Checks for any special characters for the bannerID 
        If Regex.IsMatch(bannerIDTextBox.Text, "^[A-Za-z0-9]+$") Then
            If (emailTextBox.Text Is Nothing Or emailTextBox.Text = "") Then
                lblEmail.Text = "PLEASE ENTER EMAIL BEFORE SUBMIT"
            ElseIf (bannerIDTextBox.Text Is Nothing Or bannerIDTextBox.Text = "") Then
                lblBannerId.Text = "PLEASE ENTER BANNERID BEFORE SUBMIT"
            Else
                lblEmail.Text = ""
                lblBannerId.Text = ""
                email = emailTextBox.Text
                bannerID = bannerIDTextBox.Text
                authentication()
            End If
        Else
            lblNoAttempt.Text = "INVALID CHARACTERS PLEASE TRY AGAIN"
        End If

    End Sub

    Protected Sub authentication()
        Dim dsAllStudents As DataSet = New DataSet()
        dsAllStudents = quiz.getAllStudents()
        Dim validatedStudent As Boolean = False
        Dim studentAttempt As Integer = -1
        Dim passFail As Boolean = False
        'Checks current students credentials against the master table of every student for that semester
        For index As Integer = 1 To dsAllStudents.Tables(0).Rows.Count
            If (bannerID.ToLower() = dsAllStudents.Tables(0).Rows(index - 1)("banner_id").ToString().ToLower()) Then
                If (email.ToLower() = dsAllStudents.Tables(0).Rows(index - 1)("email").ToString().ToLower()) Then
                    validatedStudent = True
                End If
            End If
        Next

        Dim dsQuizStudents As DataSet = New DataSet()
        dsQuizStudents = quiz.getQuizStudents()
        If (dsQuizStudents.Tables(0).Rows.Count >= 1) Then
            For index As Integer = 1 To dsQuizStudents.Tables(0).Rows.Count
                If (bannerID = dsQuizStudents.Tables(0).Rows(index - 1)("banner_id").ToString()) Then
                    If (email = dsQuizStudents.Tables(0).Rows(index - 1)("email").ToString()) Then
                        'Student has taken the quiz before and has ran out of attempts
                        studentAttempt = 2
                        If (dsQuizStudents.Tables(0).Rows(index - 1)("Attempts").ToString() < 2) Then
                            'Student has taken the quiz only once
                            studentAttempt = 1
                        End If

                        If (dsQuizStudents.Tables(0).Rows(index - 1)("pass").ToString() = "Y") Then
                            passFail = True
                        End If
                    End If
                End If
            Next
        End If

        If Request.Cookies(bannerID) Is Nothing Then
            Session("Email") = email
            Session("bannerID") = bannerID
            Session("studentAttempt") = studentAttempt
            If (studentAttempt <> 2 And validatedStudent And passFail = False) Then
                'Student has taken the quiz before and has attempts and has not passed
                Dim aCookie As New HttpCookie(bannerID)
                aCookie.Value = bannerID
                aCookie.Expires = DateTime.Now.AddMinutes(15)
                Response.Cookies.Add(aCookie)
                Response.Redirect("QuizTester.aspx")
            ElseIf (studentAttempt = 2 And validatedStudent And passFail = False) Then
                'Student has ran out of attempts 
                lblNoAttempt.Text = "Your 2 attempts have been used. Please see CSSA staff."
            ElseIf (passFail = True And validatedStudent) Then
                'Student passed quiz previously
                lblNoAttempt.Text = "You have already passed the quiz! Please check your JCU email for more information or contact CSSA"
            Else
                lblNoAttempt.Text = "Email or BannerID not found. Please verify and resubmit"
            End If
        Else
            lblNoAttempt.Text = "You have already signed in! please wait fifteen minutes before logging in."
        End If

       
       
    End Sub


End Class